Analyze the life expectancy of various countries over the years by using factors like population and income.
My focus has been on analysis life expectancy across various regions. I have tried to answer questions like what is the mean, density of life expectancy across the various regions through these years. How has it changed over the years and what it says.
Apart from that I have also focused on analyzing the United States data, trying to analyze how the population, income, life expectany has changes over the course of years.
library(readr)
library(ggplot2)
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(ggthemes)
gapminder <- read_csv("~/Desktop/RExercises/gapminder.csv")
## Parsed with column specification:
## cols(
## Country = col_character(),
## Year = col_integer(),
## life = col_double(),
## population = col_number(),
## income = col_double(),
## region = col_character()
## )
# View(gapminder)
Our data consists of the following variables:Country, Year, Life, Population, Income and Region
names(gapminder)
## [1] "Country" "Year" "life" "population" "income"
## [6] "region"
Let us have a look at the our data.
summary(gapminder)
## Country Year life population
## Length:41284 Min. :1800 Min. : 1.00 Min. :1.548e+03
## Class :character 1st Qu.:1854 1st Qu.:31.00 1st Qu.:5.335e+05
## Mode :character Median :1908 Median :35.12 Median :3.358e+06
## Mean :1907 Mean :42.88 Mean :2.119e+07
## 3rd Qu.:1962 3rd Qu.:55.60 3rd Qu.:1.078e+07
## Max. :2015 Max. :84.10 Max. :1.376e+09
## NA's :25817
## income region
## Min. : 142 Length:41284
## 1st Qu.: 883 Class :character
## Median : 1450 Mode :character
## Mean : 4571
## 3rd Qu.: 3483
## Max. :182668
## NA's :2341
Let us have a close look at the structure of our data. You will observe that country, region are factor type variables whereas Year, life, population and income are int type variables. Also, let us clean our data to remove NA values.
str(gapminder)
## Classes 'tbl_df', 'tbl' and 'data.frame': 41284 obs. of 6 variables:
## $ Country : chr "Afghanistan" "Afghanistan" "Afghanistan" "Afghanistan" ...
## $ Year : int 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 ...
## $ life : num 28.2 28.2 28.2 28.2 28.2 ...
## $ population: num 3280000 NA NA NA NA NA NA NA NA NA ...
## $ income : num 603 603 603 603 603 603 603 603 603 603 ...
## $ region : chr "South Asia" "South Asia" "South Asia" "South Asia" ...
## - attr(*, "spec")=List of 2
## ..$ cols :List of 6
## .. ..$ Country : list()
## .. .. ..- attr(*, "class")= chr "collector_character" "collector"
## .. ..$ Year : list()
## .. .. ..- attr(*, "class")= chr "collector_integer" "collector"
## .. ..$ life : list()
## .. .. ..- attr(*, "class")= chr "collector_double" "collector"
## .. ..$ population: list()
## .. .. ..- attr(*, "class")= chr "collector_number" "collector"
## .. ..$ income : list()
## .. .. ..- attr(*, "class")= chr "collector_double" "collector"
## .. ..$ region : list()
## .. .. ..- attr(*, "class")= chr "collector_character" "collector"
## ..$ default: list()
## .. ..- attr(*, "class")= chr "collector_guess" "collector"
## ..- attr(*, "class")= chr "col_spec"
Lets look at the mean life expentancy across the six different regions. For the below plot we can infer that the life expectancy across various regions lie roughly between 50 to 70 years. Sub Saharan African region had the least life expectancy and Europe & Central asian had the highest life expectancy.
mean(gapminder$life)
## [1] 42.88477
aggregate(life ~ region, gapminder, mean)
## region life
## 1 America 44.54065
## 2 East Asia & Pacific 41.76041
## 3 Europe & Central Asia 48.79419
## 4 Middle East & North Africa 41.55366
## 5 South Asia 37.42302
## 6 Sub-Saharan Africa 37.88242
ggplot(na.omit(gapminder), aes(x = region, y = life)) +
geom_boxplot(outlier.colour = "red") +
ggtitle("Mean of life expectancy across different regions") +
xlab("Regions") + ylab("Life Expectancy")
I wanted to explore a little more about how the density of life expectancy changed over the years, by comparing years 1800 to 2015. There has been a huge difference since 1800’s. Life expectancy rate has definitely increase over the years and there has been a change of life expectancy rates across various regions as well.
na.omit(gapminder) %>%
filter(Year==1800) %>%
ggplot(aes(x = life, fill = region)) +
geom_density(alpha = 0.2) +
ggtitle("Density of life expectancy across different regions in the year 1800") +
ylab("Density") + xlab("Life Expectancy")
na.omit(gapminder) %>%
filter(Year==2015) %>%
ggplot(aes(x = life, fill = region)) +
geom_density(alpha = 0.2) +
ggtitle("Density of life expectancy across different regions in the year 2015") +
ylab("Density") + xlab("Life Expectancy")
gapminderData <- na.omit(gapminder) %>% filter(Year == 2015) %>% group_by(region)
ggplot(data= gapminderData, mapping = aes(y=region, x=income))+ geom_point()+ geom_jitter()+ coord_cartesian(ylim=c(0, 7))
p <- ggplot(na.omit(gapminder), aes(income, life)) +
scale_x_log10() +
aes(col=region) + geom_point() + geom_smooth(lwd=2, se=FALSE) +
ggtitle("Life expectancy vs Income by Region") +
xlab("Income") + ylab("Life Expectancy (years)") +
theme_gray() +
theme_bw() +
theme_classic()
p + theme_excel()
## `geom_smooth()` using method = 'gam' and formula 'y ~ s(x, bs = "cs")'
p + theme_excel() + scale_colour_excel()
## `geom_smooth()` using method = 'gam' and formula 'y ~ s(x, bs = "cs")'
p + theme_gdocs() + scale_colour_gdocs()
## `geom_smooth()` using method = 'gam' and formula 'y ~ s(x, bs = "cs")'
p + theme_stata() + scale_colour_stata()
## `geom_smooth()` using method = 'gam' and formula 'y ~ s(x, bs = "cs")'
I wanted to check if the population affected the income earned across the various regions but from the below graph we can say that there has been not much of an affect of both the factors on each other.
unique(gapminder$Year)
## [1] 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813
## [15] 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827
## [29] 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841
## [43] 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855
## [57] 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869
## [71] 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883
## [85] 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897
## [99] 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911
## [113] 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925
## [127] 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939
## [141] 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953
## [155] 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967
## [169] 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981
## [183] 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995
## [197] 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009
## [211] 2010 2011 2012 2013 2014 2015
x <- na.omit(gapminder) %>%
filter(Year==2015) %>%
ggplot( aes(income, population, size = 0.1, color=region)) +
geom_point() +
scale_x_log10() +
theme_bw() +
ggtitle("Population comparison to Income across different regions") +
xlab("Income") + ylab("Population")
ggplotly(x)
Lastly, I wanted to compare the how population, income and life expectancy varied across America over all these years. As observed by the graphs below, the life expectancy across america has increased tremendously over the years, so has the population and income. As and how the population seems to be growing the income seems to increase as well.
USA <- (gapminder) %>%
filter(Country=="United States")
plot(USA$life, USA$Year, type = "s", xlab = "Life Expectancy", ylab = "Year", main ="Life expectancy in America across the years")
plot(USA$population, USA$Year, las=1, xlab = "Population", ylab = "Year", main ="Population in America across the years", bty="n", cex=0.5, cex.axis=0.6, pch=19)
plot(USA$income, USA$Year, type = "l", xlab = "Income", ylab = "Year", main ="Incomein America across the years")